﻿$(document).ready(function () {
    var mainPage = MainInit();
    mainPage.Init();
});

function MainInit() {
    var contextMenu;
    var editWindow;
    var deleteDialog;
    var userTable;
    var newUserWindow;

    function initBinding() {
        contextMenu.bind('itemclick', function(event) {
            var args = event.args;
            var userLogin = userTable.GetSelectedLogin();
            if (args.id == "editMenuItem") {
                $.ajax({
                    url: '/data/user/',
                    data: { login: userLogin },
                    type: "GET",
                    contentType: 'application/json; charset=utf-8',
                }).done(function (data) {
                    if (data != null) {
                        //userModel = data;
                        newUserWindow.SetModel(data);
                        newUserWindow.ShowDialog(false);
                    } else {
                        alert('К данным нет доступа', "Внимание");
                    }
                }).error(function () {
                    alert('Проверьте подключение к интернету и обновите окно.', "Ошибка связи");
                });

                
                //editWindow.OpenDialog(userLogin);
            } else {
                deleteDialog.openDialog(userLogin);
            }
            userTable.ClearSelection();
        });

        userTable.UserTable.bind('rowclick', function(event) {
            if (event.args.rightclick) {
                userTable.UserTable.jqxGrid('selectrow', event.args.rowindex);
                var rowindex = userTable.GetSelectedIndex();
                if (rowindex != -1) {
                    var scrollTop = $(window).scrollTop();
                    var scrollLeft = $(window).scrollLeft();
                    contextMenu.jqxMenu('open', parseInt(event.args.originalEvent.clientX) + 5 + scrollLeft, parseInt(event.args.originalEvent.clientY) + 5 + scrollTop);
                }
                return false;
            }
        });
        
        $('#createUserTask').click(function () {
            newUserWindow.ShowDialog(true);
        });
    };
    
    return {
        Init: function () {
            contextMenu = $("#userMenu").jqxMenu({
                width: 100,
                height: 58,
                autoOpenPopup: false,
                mode: 'popup',
                theme: theme
            });
            //$('#mainSplitter').jqxSplitter({
            //    width: '100%',
            //    height: '100%',
            //    theme: theme,
            //    panels: [{ size: 300 }]
            //});
            //$(".navigationBar").jqxNavigationBar({
            //    width: '100%',
            //    height: 'auto',
            //    expandedIndexes: [0],
            //    expandMode: 'single',
            //    theme: theme,
            //    sizeMode: "fitAvailableHeight"
            //});
            
            $('#createUserTask').jqxButton({ width: '200px', theme: theme, heigh: 50 });
            //$(".mainMenu").jqxMenu({ width: '100%', mode: 'vertical', theme: theme });
            //$(".mainMenu").css('visibility', 'visible');
            

            deleteDialog = DeleteDialog();
            userTable = MainTable();
            deleteDialog.init(userTable);
            
            newUserWindow = NewUserWindow();
            newUserWindow.Init();
            initBinding();
        }
    };
};


